package com.yanqu.road.server.manger.maidian;

import com.yanqu.road.entity.curio.UserCurioData;
import com.yanqu.road.entity.curio.UserCurioItemData;
import com.yanqu.road.entity.curio.UserCurioSkillData;
import com.yanqu.road.entity.curio.config.CurioItemSuitInfo;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.curio.CurioModule;
import com.yanqu.road.server.gameplayer.module.patrons.CombLeaderLog;
import com.yanqu.road.server.gameplayer.module.vehicle.log.VehicleBoneLog;
import com.yanqu.road.server.manger.DataAnalyticsMgr;
import com.yanqu.road.server.manger.activity.curio.CurioConfigMgr;
import com.yanqu.road.server.manger.maidian.beauty.BeautyLog;
import com.yanqu.road.server.manger.maidian.cricket.CricketLog;
import com.yanqu.road.server.manger.maidian.embroidroom.EmbroidRoomLog;
import com.yanqu.road.server.manger.maidian.grandchild.GrandChildLog;
import com.yanqu.road.server.manger.maidian.hospitalroom.HospitalRoomLog;
import com.yanqu.road.server.manger.maidian.house.HouseLog;
import com.yanqu.road.server.manger.maidian.innroom.InnRoomLog;
import com.yanqu.road.server.manger.maidian.mammon.MammomLog;
import com.yanqu.road.server.manger.maidian.manor.ManorFishLog;
import com.yanqu.road.server.manger.maidian.mansionroom.MansionRoomLog;
import com.yanqu.road.server.manger.maidian.medicineroom.MedicineRoom;
import com.yanqu.road.server.manger.maidian.patrons.PatronsLog;
import com.yanqu.road.server.manger.maidian.pet.PetLog;
import com.yanqu.road.server.manger.maidian.resource.BuyItemLog;
import com.yanqu.road.server.manger.maidian.restaurantroom.RestaurantRoomLog;
import com.yanqu.road.server.manger.maidian.shopsroom.ShopsRoomLog;
import com.yanqu.road.server.manger.maidian.travelbook.TravelBookLog;
import com.yanqu.road.server.manger.maidian.wineroom.WineryRoomLog;
import com.yanqu.road.server.manger.maidian.wingroom.WingRoomLog;
import com.yanqu.road.utils.executor.GeneralTask;

import java.util.*;

/**
 * 每日玩家首次登录
 */
public class DayFirstLoginTask extends GeneralTask {

    GamePlayer player;

    public DayFirstLoginTask(GamePlayer player) {
        this.player = player;
    }

    @Override
    public void doRun() {

        //厢房快照
        WingRoomLog.uploadSnapshot(player);

        //山庄快照
        GrandChildLog.uploadSnapshot(player);

        //庄园快照
        ManorFishLog.uploadSnapshot(player);

        //钱庄快照
        HouseLog.uploadSnapshot(player);

        //商铺快照
        ShopsRoomLog.uploadSnapshot(player);

        //医馆快照
        HospitalRoomLog.uploadSnapshot(player);

        //药铺快照
        MedicineRoom.uploadSnapshot(player);

        //酒肆快照
        RestaurantRoomLog.uploadSnapshot(player);

        //酒坊快照
        WineryRoomLog.uploadSnapshot(player);

        //客栈快照
        InnRoomLog.uploadSnapshot(player);

        //府邸外观快照
        MansionRoomLog.uploadSnapshot(player);

        //冷宫快照
        EmbroidRoomLog.uploadSnapshot(player);

        //蛐蛐基础快照
        CricketLog.uploadCricketBaseInfoSnapshot(player);

        //鱼塘基础快照
        ManorFishLog.uploadBaseInfoSnapshot(player);

        //背包快照
        BuyItemLog.trackUserBagSnapshot(player);

        //上传门客信息快照
        PatronsLog.trackPatronsInfoSnapshot(player);

        //上传美女信息快照
        BeautyLog.trackBeautyInfoSnapshot(player);

        //上传珍兽信息快照
        PetLog.trackPetInfoSnapshot(player);

        //鱼快照
        ManorFishLog.trackInfoSnapshot(player);

        //蛐蛐快照
        CricketLog.trackInfoSnapshot(player);

        //财神快照
        MammomLog.trackSnapshot(player);

        //魂骨
        VehicleBoneLog.traceLoginBoneLog(player);

        // 凤魁系统
        CombLeaderLog.traceLoginLog(player);

        // 旅行册快照
        TravelBookLog.trackTravelBookInfoSnapshot(player);

        //藏宝快照
        //临时热更
        try {
            if (DataAnalyticsMgr.openDataAnalytics()){
                DefaultEventTraceAction action = new DefaultEventTraceAction("treasure_user", player);
                CurioModule module = player.getModule(CurioModule.class);
                UserCurioData userCurioData = module.getUserCurioData();
                if(userCurioData != null){
                    action.build("treasure_level", userCurioData.getBadgeLv());
                    Map<Integer, UserCurioItemData> curioItemDataMap = module.getCurioItemDataMap();
                    action.build("treasure_nums", curioItemDataMap.size());
                    action.build("treasure_score", userCurioData.getScore());

                    Map<Integer, UserCurioSkillData> skillDataMap = module.getSkillDataMap();

                    Set<Integer> suitSet = new HashSet<>();
                    for (Map.Entry<Integer, UserCurioItemData> entry : curioItemDataMap.entrySet()) {
                        suitSet.addAll(CurioConfigMgr.getConfig().getSuitList(entry.getKey()));
                    }
                    List<Integer> suitUnLock = new ArrayList<>();
                    for (Integer suitId : suitSet) {
                        CurioItemSuitInfo suitInfo = CurioConfigMgr.getConfig().getCurioItemSuitInfo(suitId);
                        if(suitInfo != null){
                            if(skillDataMap.containsKey(suitInfo.getSkill())){
                                if(!suitUnLock.contains(suitInfo.getId())){
                                    suitUnLock.add(suitInfo.getId());
                                }
                            }
                        }
                    }
                    //解锁的套装数
                    action.build("treasure_suitnum", suitUnLock.size());

                    //效果总览
                    List<Map<String, Object>> groupMap = new ArrayList<>();
                    for (Map.Entry<Integer, UserCurioSkillData> entry : skillDataMap.entrySet()) {
                        UserCurioSkillData skillData = entry.getValue();
                        Map<String, Object> map = new HashMap<>();
                        map.put("treasure_addid", skillData.getSkill());
                        map.put("treasure_addnum", skillData.getAddition());
                        groupMap.add(map);
                    }
                    action.build("treasure_add", groupMap);

                    List<Map<String, Object>> itemMap = new ArrayList<>();
                    for (Map.Entry<Integer, UserCurioItemData> entry : curioItemDataMap.entrySet()) {
                        UserCurioItemData itemData = entry.getValue();
                        Map<String, Object> map = new HashMap<>();
                        map.put("treasure_id", itemData.getItemId());
                        map.put("treasure_lev", itemData.getLv());
                        map.put("treasure_star", itemData.getStarLv());
                        itemMap.add(map);
                    }
                    action.build("treasure_growmap", itemMap);

                    List<Map<String, Object>> suitMap = new ArrayList<>();
                    for (Map.Entry<Integer, UserCurioSkillData> entry : skillDataMap.entrySet()) {
                        UserCurioSkillData skillData = entry.getValue();
                        if(skillData.getSkillType() != 3){
                            continue;
                        }
                        Map<String, Object> map = new HashMap<>();
                        map.put("treasure_suitid", skillData.getObjId());
                        map.put("treasure_suitlev", skillData.getLv());
                        suitMap.add(map);
                    }
                    action.build("treasure_suitmap", suitMap);

                    action.trace();
                }
            }

        }catch (Exception e){
            DataAnalyticsMgr.getLogger().error(" error ", e);
        }

    }
}
